package com.frogs.ucenter.component;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.frogs.system.util.UserMessageTypeEnum;
import com.frogs.ucenter.v1.request.DirectMessageSendRequest;
import com.frogs.ucenter.v1.service.DirectMessageService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Component("messageHelper")
public class UserMessageDmHelper {
	
	@Resource(name=ApiConstants.VERSION+"directMessageService")
	private DirectMessageService directMessageServiceImpl;
	
	/**
	 * 发送指定用户站内短信
	 * @param subject
	 * @param content
	 * @param sendTime
	 * @param overTime
	 * @param sender
	 * @param senderId
	 * @param receiverArray
	 * @param receiverType   见{@link UserMessageTypeEnum}
	 * @param applicableId
	 * @throws Exception 
	 */
	@Async
	public void sendToUser(String subject, String content, String sendTime, 
			String overTime,String sender, String senderId,
			JSONArray receiverArray, int receiverType, int applicableId) throws Exception {
		DirectMessageSendRequest sendRequest = new DirectMessageSendRequest();
		sendRequest.setSubject(subject);
		sendRequest.setApplicableId(applicableId);
		sendRequest.setContent(content);
		sendRequest.setOverTime(overTime);
		sendRequest.setReceiverType(receiverType);
		sendRequest.setReceiverArray(receiverArray);
		sendRequest.setSender(sender);
		sendRequest.setSenderId(senderId);
		sendRequest.setSendTime(sendTime);
		sendRequest.setSendType(UserMessageTypeEnum.SEND_SYSTEM.getId());
		
		directMessageServiceImpl.send(sendRequest);
	}
	
	/**
	 * 发送全场消息
	 * @param subject
	 * @param content
	 * @param sendTime
	 * @param overTime
	 * @param sender
	 * @param applicableId
	 * @throws Exception 
	 */
	public void sendToAllUser(String subject, String content, String sendTime, 
			String overTime,String sender, int applicableId) throws Exception {
		
		DirectMessageSendRequest sendRequest = new DirectMessageSendRequest();
		JSONArray receiverArray = new JSONArray();
		
		JSONObject receiverObj = new JSONObject();
		receiverObj.put("receiver", "");
		receiverObj.put("receiverId", "-1");
		receiverArray.add(receiverObj);
		
		sendRequest.setApplicableId(applicableId);
		sendRequest.setContent(content);
		sendRequest.setOverTime(overTime);
		sendRequest.setReceiverType(UserMessageTypeEnum.RECEIVER_ALL.getId());
		
		sendRequest.setReceiverArray(receiverArray);
		sendRequest.setSender(sender);
		sendRequest.setSenderId("-1");
		sendRequest.setSendTime(sendTime);
		sendRequest.setSendType(UserMessageTypeEnum.SEND_SYSTEM.getId());
		
		directMessageServiceImpl.send(sendRequest);
	}
	
}
